#include "CIO.h"

#include "../CGlobal.h"
#include "../CCoreException.h"
#include "../CVMDataTypes.h"


void CPrint::print(CFrame& frame, QStack<CFrame*>& frames)
{
    if (!frame.variables().contains(CPrint::parameter1))
        throw CCoreException(CCoreException::VM_EXCEPTION_INSUFFICIENT_OPERANDS, "<functions.io.print>");

    const char* vrbl = frame.variables()[CPrint::parameter1];

    switch((CVMDataTypes)*vrbl)
    {
        case CVMDataTypes::NULL_:
        {
            CGlobal::output << "null";
            break;
        }
        case CVMDataTypes::FALSE:
        {
            CGlobal::output << "false";
            break;
        }
        case CVMDataTypes::TRUE:
        {
            CGlobal::output << "true";
            break;
        }
        case CVMDataTypes::NUMBER:
        {
            CGlobal::output << *(double*)(vrbl + 1);
            break;
        }
        default:
            throw CCoreException(CCoreException::VM_EXCEPTION_WRONG_OPERAND, "<functions.io.print>");
    }

    frames.top()->pushNull();
}

void CPrintln::println(CFrame& frame, QStack<CFrame*>& frames)
{
    CPrint::print(frame, frames);
    CGlobal::output << endl;
}
